#Include DrawBase.ahk

class DrawFree extends DrawBase {

  size := 2, color := '0xffffc800'

  __New() {
    this.lastX := -1, this.lastY := -1
    this.data := []
  }

  UpdateParam(size, color) {
    this.size := size, this.color := color
  }

  BeginDraw(*) {
    this.data := []
  }

  EndDraw(*) {
    this.lastX := this.lastY := -1
    return this.data
  }

  Draw(overlay, pos, *) {
    super.Draw(overlay, pos)
    lastX := this.lastX, lastY := this.lastY

    if (overlay.GetMousePos(&mx, &my)) {
      if (lastX != -1 and (lastX != mx or lastY != my)) {
        this.data.push(DrawFree.mouseTrail(lastX, lastY, mx, my, this.size, this.color))
      }
      lastX := mx, lastY := my
    } else {
      lastX := lastY := -1
    }
    this.lastX := lastX, this.lastY := lastY
    this.data.foreach(y => y.draw(overlay))
  }

  static Render(overlay, data, *) {
    data.foreach(v => v.draw(overlay))
  }


  class mouseTrail {
    __New(x1, y1, x2, y2, size, color) {
      this.x1 := x1, this.y1 := y1, this.x2 := x2, this.y2 := y2
      this.size := size, this.alpha := (color & 0xFF000000) >> 24, this.rgb := (color & 0xFFFFFF)
    }

    Draw(o) {
      if (this.alpha < 1)
        return 0
      o.DrawLine(this.x1, this.y1, this.x2, this.y2, (this.alpha << 24) + this.rgb, this.size, 1)
      return 1
    }
  }
}